package hamsterwrench.data.reload;

import java.io.IOException;
import java.io.RandomAccessFile;

class LittleEndianRAF{
	
	private RandomAccessFile raf;
	public LittleEndianRAF(RandomAccessFile in){
		raf = in;
	}
	
	public short readShort() throws IOException{
		short value = raf.readShort();
		int b1 = value & 0xff;
	    int b2 = (value >> 8) & 0xff;

	    return (short) (b1 << 8 | b2 << 0);
	}
	
	public void writeShort(short value) throws IOException{
		int b1 = value & 0xff;
	    int b2 = (value >> 8) & 0xff;

	    raf.writeShort(b1 << 8 | b2 << 0);
	}
	
	public int readInt() throws IOException{
		int value = raf.readInt();
		int b1 = (value >>  0) & 0xff;
	    int b2 = (value >>  8) & 0xff;
	    int b3 = (value >> 16) & 0xff;
	    int b4 = (value >> 24) & 0xff;

	    return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
	}
	
	public void writeInt(int value) throws IOException{
		int b1 = (value >>  0) & 0xff;
	    int b2 = (value >>  8) & 0xff;
	    int b3 = (value >> 16) & 0xff;
	    int b4 = (value >> 24) & 0xff;

	    raf.writeInt(b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0);
	}
	
	public long readLong() throws IOException{
		long value = raf.readLong();
		long b1 = (value >>  0) & 0xff;
	    long b2 = (value >>  8) & 0xff;
	    long b3 = (value >> 16) & 0xff;
	    long b4 = (value >> 24) & 0xff;
	    long b5 = (value >> 32) & 0xff;
	    long b6 = (value >> 40) & 0xff;
	    long b7 = (value >> 48) & 0xff;
	    long b8 = (value >> 56) & 0xff;

	    return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 |
	           b5 << 24 | b6 << 16 | b7 <<  8 | b8 <<  0;
	}
	
	public void writeLong(long value) throws IOException{
		long b1 = (value >>  0) & 0xff;
	    long b2 = (value >>  8) & 0xff;
	    long b3 = (value >> 16) & 0xff;
	    long b4 = (value >> 24) & 0xff;
	    long b5 = (value >> 32) & 0xff;
	    long b6 = (value >> 40) & 0xff;
	    long b7 = (value >> 48) & 0xff;
	    long b8 = (value >> 56) & 0xff;

	    raf.writeLong(b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 |
	           b5 << 24 | b6 << 16 | b7 <<  8 | b8 <<  0);
	}
	
	public double readDouble() throws IOException{
		long longValue = Double.doubleToLongBits (raf.readDouble());
	    longValue = swap (longValue);
	    return Double.longBitsToDouble (longValue);
	}
	
	public void writeDouble(double value) throws IOException{
		long longValue = Double.doubleToLongBits (value);
	    longValue = swap (longValue);
	    raf.writeDouble(Double.longBitsToDouble (longValue));
	}
	
	private long swap(long value){
		long b1 = (value >>  0) & 0xff;
	    long b2 = (value >>  8) & 0xff;
	    long b3 = (value >> 16) & 0xff;
	    long b4 = (value >> 24) & 0xff;
	    long b5 = (value >> 32) & 0xff;
	    long b6 = (value >> 40) & 0xff;
	    long b7 = (value >> 48) & 0xff;
	    long b8 = (value >> 56) & 0xff;

	    return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 |
	           b5 << 24 | b6 << 16 | b7 <<  8 | b8 <<  0;
	}
	
}